#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MEMORY_SIZE 1024 // 假设内存大小为1024字节

typedef struct Block
{
    size_t size;
    struct Block *next;
    int free;
} Block;

static Block memory[MEMORY_SIZE];
static Block *free_list = NULL;

void *my_malloc(size_t size)
{
    Block *prev = NULL, *curr = free_list;
    while (curr)
    {
        if (curr->free && curr->size >= size)
        {
            curr->free = 0;
            return (void *)(curr + 1);
        }
        prev = curr;
        curr = curr->next;
    }
    return NULL;
}

void my_free(void *ptr)
{
    if (!ptr)
        return;
    Block *block = (Block *)ptr - 1;
    block->free = 1;
}

int main()
{
    // 初始化内存块
    memory[0].size = MEMORY_SIZE - sizeof(Block);
    memory[0].next = NULL;
    memory[0].free = 1;
    free_list = memory;

    char *str = (char *)my_malloc(100);
    if (str)
    {
        strcpy(str, "Hello, World!");
        printf("%s\n", str);
        my_free(str);
    }

    return 0;
}